package com.hzhjhh.view.manageView.menus.purchase.purchaseOrder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.entity.Stock;
import com.hzhjhh.mapper.StockMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.purchase.PurchaseOrderDialog;

/**
 * 菜单栏初始化-采购-采购表弹出框-提交按钮
 * 
 * @author 12083
 *
 */
public class SubmitBt {

	private static StockMapper stockMapper;
	
	/**
	 * 按钮
	 */
	public static JButton btInit(PurchaseOrderDialog stockDialog) {
		JButton submit = new JButton();
		submit.setBounds(351, 93, 83, 25);
		submit.setText("提交");
		submit.addActionListener(event -> {
			Vector<Vector<String>> inserAll = stockDialog.getInserAll();
			int i = 0;
			int insert = 0;
			if (inserAll.size() == 0) {
				JOptionPane.showMessageDialog(stockDialog, "没有可提交的数据", "提示", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			List<Stock> stockList = new ArrayList<>();
			Date now = new Date();
			while (i < inserAll.size()) {
				Vector inserData = new Vector();
				inserData = (Vector) inserAll.get(i);
				Stock stock = new Stock();
				stock.setMerchandiseCode(inserData.get(0).toString());
				stock.setMerchandiseName(inserData.get(1).toString());
				stock.setDept(inserData.get(2).toString());
				stock.setNumber(Integer.parseInt(inserData.get(3).toString()));
				stock.setPricePerOne(inserData.get(4).toString());
				stock.setTotalAmt(inserData.get(5).toString());
				stock.setOrderDate(inserData.get(6).toString());
				stock.setPaymentTerm(inserData.get(7).toString());
				stock.setCreatedDate(now);
				stock.setCreatedBy("sys");
				stock.setUpdatedDate(now);
				stock.setUpdatedBy("sys");
				stock.setIsDeleted(0);
				
				stockList.add(stock);
				i++;
			}
			if (CollectionUtils.isNotEmpty(stockList)) {
				stockMapper.batchAdd(stockList);
			}
		});
		
		// 初始化mapper
		SqlSession sqlSession = DBUtils.getSqlSession(true);
		stockMapper = sqlSession.getMapper(StockMapper.class);
		 
		return submit;
	}
}
